home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 1.5)
-
- '''Common pathname manipulations, Posix version.
- Instead of importing this module
- directly, import os and refer to this module as os.path.
- '''
- import os
- import stat
-
- def normcase(s):
- '''Normalize case of pathname. Has no effect under Posix'''
- return s
-
-
- def isabs(s):
- '''Test whether a path is absolute'''
- return s[:1] == '/'
-
-
- def join(a, *p):
- """Join two or more pathname components, inserting '/' as needed"""
- path = a
- for b in p:
- if b[:1] == '/':
- path = b
- elif path == '' or path[-1:] == '/':
- path = path + b
- else:
- path = path + '/' + b
-
- return path
-
-
- def split(p):
- '''Split a pathname. Returns tuple "(head, tail)" where "tail" is
- everything after the final slash. Either part may be empty'''
- import string
- i = string.rfind(p, '/') + 1
- (head, tail) = (p[:i], p[i:])
- if head and head != '/' * len(head):
- while head[-1] == '/':
- head = head[:-1]
-
- return (head, tail)
-
-
- def splitext(p):
- '''Split the extension from a pathname. Extension is everything from the
- last dot to the end. Returns "(root, ext)", either part may be empty'''
- (root, ext) = ('', '')
- for c in p:
- if c == '/':
- (root, ext) = (root + ext + c, '')
- elif c == '.':
- if ext:
- (root, ext) = (root + ext, c)
- else:
- ext = c
- elif ext:
- ext = ext + c
- else:
- root = root + c
-
- return (root, ext)
-
-
- def splitdrive(p):
- '''Split a pathname into drive and path. On Posix, drive is always
- empty'''
- return ('', p)
-
-
- def basename(p):
- '''Returns the final component of a pathname'''
- return split(p)[1]
-
-
- def dirname(p):
- '''Returns the directory component of a pathname'''
- return split(p)[0]
-
-
- def commonprefix(m):
- '''Given a list of pathnames, returns the longest common leading component'''
- if not m:
- return ''
-
- prefix = m[0]
- for item in m:
- for i in range(len(prefix)):
- pass
-
-
- return prefix
-
-
- def getsize(filename):
- '''Return the size of a file, reported by os.stat().'''
- st = os.stat(filename)
- return st[stat.ST_SIZE]
-
-
- def getmtime(filename):
- '''Return the last modification time of a file, reported by os.stat().'''
- st = os.stat(filename)
- return st[stat.ST_MTIME]
-
-
- def getatime(filename):
- '''Return the last access time of a file, reported by os.stat().'''
- st = os.stat(filename)
- return st[stat.ST_MTIME]
-
-
- def islink(path):
- '''Test whether a path is a symbolic link'''
-
- try:
- st = os.lstat(path)
- except (os.error, AttributeError):
- return 0
-
- return stat.S_ISLNK(st[stat.ST_MODE])
-
-
- def exists(path):
- '''Test whether a path exists. Returns false for broken symbolic links'''
-
- try:
- st = os.stat(path)
- except os.error:
- return 0
-
- return 1
-
-
- def isdir(path):
- '''Test whether a path is a directory'''
-
- try:
- st = os.stat(path)
- except os.error:
- return 0
-
- return stat.S_ISDIR(st[stat.ST_MODE])
-
-
- def isfile(path):
- '''Test whether a path is a regular file'''
-
- try:
- st = os.stat(path)
- except os.error:
- return 0
-
- return stat.S_ISREG(st[stat.ST_MODE])
-
-
- def samefile(f1, f2):
- '''Test whether two pathnames reference the same actual file'''
- s1 = os.stat(f1)
- s2 = os.stat(f2)
- return samestat(s1, s2)
-
-
- def sameopenfile(fp1, fp2):
- '''Test whether two open file objects reference the same file'''
- s1 = os.fstat(fp1)
- s2 = os.fstat(fp2)
- return samestat(s1, s2)
-
-
- def samestat(s1, s2):
- '''Test whether two stat buffers reference the same file'''
- if s1[stat.ST_INO] == s2[stat.ST_INO]:
- pass
- return s1[stat.ST_DEV] == s2[stat.ST_DEV]
-
-
- def ismount(path):
- '''Test whether a path is a mount point'''
-
- try:
- s1 = os.stat(path)
- s2 = os.stat(join(path, '..'))
- except os.error:
- return 0
-
- dev1 = s1[stat.ST_DEV]
- dev2 = s2[stat.ST_DEV]
- if dev1 != dev2:
- return 1
-
- ino1 = s1[stat.ST_INO]
- ino2 = s2[stat.ST_INO]
- if ino1 == ino2:
- return 1
-
- return 0
-
-
- def walk(top, func, arg):
- '''walk(top,func,args) calls func(arg, d, files) for each directory "d"
- in the tree rooted at "top" (including "top" itself). "files" is a list
- of all the files and subdirs in directory "d".
- '''
-
- try:
- names = os.listdir(top)
- except os.error:
- return None
-
- func(arg, top, names)
- exceptions = ('.', '..')
- for name in names:
- if name not in exceptions:
- name = join(top, name)
- if isdir(name) and not islink(name):
- walk(name, func, arg)
-
-
-
-
-
- def expanduser(path):
- '''Expand ~ and ~user constructions. If user or $HOME is unknown,
- do nothing'''
- if path[:1] != '~':
- return path
-
- (i, n) = (1, len(path))
- while i < n and path[i] != '/':
- i = i + 1
- if i == 1:
- if not os.environ.has_key('HOME'):
- return path
-
- userhome = os.environ['HOME']
- else:
- import pwd
-
- try:
- pwent = pwd.getpwnam(path[1:i])
- except KeyError:
- return path
-
- userhome = pwent[5]
- if userhome[-1:] == '/':
- i = i + 1
-
- return userhome + path[i:]
-
- _varprog = None
-
- def expandvars(path):
- '''Expand shell variables of form $var and ${var}. Unknown variables
- are left unchanged'''
- global _varprog
- if '$' not in path:
- return path
-
- if not _varprog:
- import re
- _varprog = re.compile('\\$(\\w+|\\{[^}]*\\})')
-
- i = 0
- while 1:
- m = _varprog.search(path, i)
- if not m:
- break
-
- (i, j) = m.span(0)
- name = m.group(1)
- if name[:1] == '{' and name[-1:] == '}':
- name = name[1:-1]
-
- if os.environ.has_key(name):
- tail = path[j:]
- path = path[:i] + os.environ[name]
- i = len(path)
- path = path + tail
- else:
- i = j
- return path
-
-
- def normpath(path):
- '''Normalize path, eliminating double slashes, etc.'''
- import string
- slashes = ''
- while path[:1] == '/':
- slashes = slashes + '/'
- path = path[1:]
- comps = string.splitfields(path, '/')
- i = 0
- while i < len(comps):
- if comps[i] == '.':
- del comps[i]
- while i < len(comps) and comps[i] == '':
- del comps[i]
- elif comps[i] == '..' and i > 0 and comps[i - 1] not in ('', '..'):
- del comps[i - 1:i + 1]
- i = i - 1
- elif comps[i] == '' and i > 0 and comps[i - 1] != '':
- del comps[i]
- else:
- i = i + 1
- if not comps and not slashes:
- comps.append('.')
-
- return slashes + string.joinfields(comps, '/')
-
-
- def abspath(path):
- if not isabs(path):
- path = join(os.getcwd(), path)
-
- return normpath(path)
-
-